package xm.chengdu.com.beijing_211;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.czy1121.view.RoundButton;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import com.sdwfqin.cbt.Content;
import utils.FaSong;

/**
 * 当前状态
 */
public class CurrentActivity extends Activity {


    @BindView(R.id.home)
    RoundButton home;
    @BindView(R.id.stop)
    RoundButton stop;
    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.textView4)
    TextView textView4;

    @BindView(R.id.gongzuomoshi)
    TextView gongzuomoshi;
    @BindView(R.id.dianliang)
    TextView dianliang;
    @BindView(R.id.gecaoshengyu)
    TextView gecaoshengyu;
    @BindView(R.id.chongdianzhuangtai_mowing)
    TextView chongdianzhuangtaiMowing;
    @BindView(R.id.chongdianzhuangtai_charging)
    TextView chongdianzhuangtaiCharging;
    @BindView(R.id.chongdianzhuangtai_err)
    TextView chongdianzhuangtaiErr;
    @BindView(R.id.chongdianzhuangtai_diss)
    TextView chongdianzhuangtaiDiss;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_current);
        ButterKnife.bind(this);
//        chongdianzhuangtai.setText("Mowing state:" + Content.chongdianzhuangtai);
        gongzuomoshi.setText("Working mode:" + Content.gongzuomoshi);
        dianliang.setText("Electric quantity:" + Content.dianliang);
        gecaoshengyu.setText("Mowing surplus:" + Content.gecaoshengyu);
        if (!Content.isConn) {
            chongdianzhuangtaiDiss.setTextColor(this.getResources().getColor(R.color.white));
            chongdianzhuangtaiErr.setTextColor(this.getResources().getColor(R.color.zi_ti_qian));
            chongdianzhuangtaiCharging.setTextColor(this.getResources().getColor(R.color.zi_ti_qian));
            chongdianzhuangtaiMowing.setTextColor(this.getResources().getColor(R.color.zi_ti_qian));
        } else if (Content.ischongdian.equals("err")) {
            chongdianzhuangtaiErr.setTextColor(this.getResources().getColor(R.color.white));
            chongdianzhuangtaiDiss.setTextColor(this.getResources().getColor(R.color.zi_ti_qian));
            chongdianzhuangtaiCharging.setTextColor(this.getResources().getColor(R.color.zi_ti_qian));
            chongdianzhuangtaiMowing.setTextColor(this.getResources().getColor(R.color.zi_ti_qian));
        }else if (Content.ischongdian.equals("chong")) {
//            01 charing, 亮起
            chongdianzhuangtaiCharging.setTextColor(this.getResources().getColor(R.color.white));

            chongdianzhuangtaiErr.setTextColor(this.getResources().getColor(R.color.zi_ti_qian));
            chongdianzhuangtaiDiss.setTextColor(this.getResources().getColor(R.color.zi_ti_qian));
            chongdianzhuangtaiMowing.setTextColor(this.getResources().getColor(R.color.zi_ti_qian));

        } else if (Content.ischongdian.equals("weichong")) {
//           00 ,表示 moving
            chongdianzhuangtaiMowing.setTextColor(this.getResources().getColor(R.color.white));
            chongdianzhuangtaiCharging.setTextColor(this.getResources().getColor(R.color.zi_ti_qian));

            chongdianzhuangtaiErr.setTextColor(this.getResources().getColor(R.color.zi_ti_qian));
            chongdianzhuangtaiDiss.setTextColor(this.getResources().getColor(R.color.zi_ti_qian));
        }

        mHandler.postDelayed(r, 100);//延时100毫秒
    }



    Handler mHandler = new Handler();
    Runnable r = new Runnable() {

        @Override
        public void run() {

            if (!Content.isConn) {
                chongdianzhuangtaiDiss.setTextColor( getResources().getColor(R.color.white));
                chongdianzhuangtaiErr.setTextColor( getResources().getColor(R.color.zi_ti_qian));
                chongdianzhuangtaiCharging.setTextColor(  getResources().getColor(R.color.zi_ti_qian));
                chongdianzhuangtaiMowing.setTextColor( getResources().getColor(R.color.zi_ti_qian));
            } else if (Content.ischongdian.equals("err")) {
                chongdianzhuangtaiErr.setTextColor( getResources().getColor(R.color.white));
                chongdianzhuangtaiDiss.setTextColor( getResources().getColor(R.color.zi_ti_qian));
                chongdianzhuangtaiCharging.setTextColor( getResources().getColor(R.color.zi_ti_qian));
                chongdianzhuangtaiMowing.setTextColor( getResources().getColor(R.color.zi_ti_qian));
            }else if (Content.ischongdian.equals("chong")) {
//            01 charing, 亮起
                chongdianzhuangtaiCharging.setTextColor( getResources().getColor(R.color.white));

                chongdianzhuangtaiErr.setTextColor( getResources().getColor(R.color.zi_ti_qian));
                chongdianzhuangtaiDiss.setTextColor( getResources().getColor(R.color.zi_ti_qian));
                chongdianzhuangtaiMowing.setTextColor( getResources().getColor(R.color.zi_ti_qian));

            } else if (Content.ischongdian.equals("weichong")) {
//           00 ,表示 moving
                chongdianzhuangtaiMowing.setTextColor( getResources().getColor(R.color.white));
                chongdianzhuangtaiCharging.setTextColor( getResources().getColor(R.color.zi_ti_qian));
                chongdianzhuangtaiErr.setTextColor( getResources().getColor(R.color.zi_ti_qian));
                chongdianzhuangtaiDiss.setTextColor( getResources().getColor(R.color.zi_ti_qian));
            }

            //do something
            //每隔3 s循环执行run方法
            mHandler.postDelayed(this, 3000);
        }
    };














    @OnClick({R.id.img_back, R.id.home, R.id.stop})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.home:
                String gongzuomoshi;
                if (!Content.isConn) {
                    Toast.makeText(CurrentActivity.this, "Please connect to Bluetooth first！", Toast.LENGTH_SHORT).show();
                    return;
                }

                gongzuomoshi = FaSong.gongzuomoshi("01");

                MyEvent events4 = new MyEvent();
                events4.setType("message");
                events4.setContent(gongzuomoshi);
                EventBus.getDefault().post(events4);
                break;
            case R.id.stop:

                String gongzuomoshiss;
                if (!Content.isConn) {
                    Toast.makeText(CurrentActivity.this, "Please connect to Bluetooth first.！", Toast.LENGTH_SHORT).show();
                    return;
                }

                gongzuomoshiss = FaSong.gongzuomoshi("02");

                MyEvent events5 = new MyEvent();
                events5.setType("message");
                events5.setContent(gongzuomoshiss);
                EventBus.getDefault().post(events5);

                break;
        }
    }
}
